package cn.yunhe.service.impl;

import cn.yunhe.dao.KnowledgePointDao;
import cn.yunhe.entity.KnowledgePoint;
import cn.yunhe.service.KnowledgePointService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service("knowledgePointService")
public class KnowledgePointServiceImpl implements KnowledgePointService {
    @Resource
    private KnowledgePointDao knowledgePointDao;


    @Override
    public Page getKnowledgePointListByPage(Map<String, Object> map) {
        Page page = new Page();

        //获取总条数
        page.setTotal(knowledgePointDao.getKnowledgePointListCountByPage(map));
        // System.out.println(knowledgePointDao.getKnowledgePointListCountByPage(map));
        // 获取每页大小
        int pageSize = Integer.parseInt(String.valueOf(map.get("pageSize")));
        page.setPageSize(pageSize);

        // 获取当前页码
        int curPageNum = Integer.parseInt(String.valueOf(map.get("pageNum")));
//		page.setPageNum(curPageNum);
        // 计算出总页数
        page.setPageNum((int) (page.getTotal() / pageSize + (page.getTotal() % pageSize == 0 ? 0 : 1)));
        // 启用分页
        page = PageHelper.startPage(curPageNum, pageSize);
        // 查询列表
        List list = knowledgePointDao.getKnowledgePointListByPage(map);
        return page;
    }


    @Override
    public int addKnowledgePoint(KnowledgePoint knowledgePoint) {

        if (knowledgePoint.getPointName() == null) {
            return 0;
        } else {
            return knowledgePointDao.addKnowledgePoint(knowledgePoint);
        }
    }

    @Override
    public int delKnowledgePoint(KnowledgePoint knowledgePoint) {
        return knowledgePointDao.delKnowledgePoint(knowledgePoint);

    }

    @Override
    public int updKnowledgePoint(KnowledgePoint knowledgePoint) {
        if (knowledgePoint.getPointName() == null) {
            return 0;
        } else {
            return knowledgePointDao.updKnowledgePoint(knowledgePoint);
        }
    }

    @Override
    public KnowledgePoint getKnowledgePointById(KnowledgePoint knowledgePoint) {

        return knowledgePointDao.getKnowledgePointById(knowledgePoint);

    }


    public KnowledgePointDao getKnowledgePointDao() {
        return knowledgePointDao;
    }

    public void setKnowledgePointDao(KnowledgePointDao knowledgePointDao) {
        this.knowledgePointDao = knowledgePointDao;
    }
}
